package org.lql.algo.codecrush.week008;

import org.lql.algo.common.ListNode;

/**
 * @author: liangqinglong
 * @date: 2025-08-18 23:59
 * @description: 141. 环形链表 <a href="https://leetcode.cn/problems/linked-list-cycle/description/">...</a>
 **/
public class HasCycle {

	public boolean hasCycle(ListNode head) {
		if (head == null || head.next == null) {
			return false;
		}
		ListNode slow = head;
		ListNode fast = head.next;
		while (slow != fast) {
			if (fast == null || fast.next == null) {
				return false;
			}
			slow = slow.next;
			fast = fast.next.next;
		}
		return true;
	}

	public static void main(String[] args) {
		HasCycle hasCycle = new HasCycle();
		ListNode head = new ListNode(1);
		head.next = new ListNode(2);
		head.next.next = head;
		System.out.println(hasCycle.hasCycle(head));
	}
}
